package com.yangyouqi.chaper9.dao;

import com.yangyouqi.chaper9.domain.LearnResource;
import com.yangyouqi.chaper9.tools.Page;
import com.yangyouqi.chaper9.tools.StringUtil;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author 杨友奇
 * @Time 2018/7/26
 */
@Component
@Mapper
public interface LearnMapper {

    @Insert("insert into learn_resource(author,title,url) values(#{author},#{title},#{url})")
    int add(LearnResource learnResouce);
    @Update("update learn_resource set author=#{author},title=#{title},url=#{url} where id=#{id}")
    int update(LearnResource learnResouce);
    @DeleteProvider(type = LearnSqlBuilder.class,method = "deleteByids")
    int deleteByIds(@Param("ids") String[] ids);

    @Select("select * from learn_resource where id = #{id}")
    @Results(id = "learnMap",value = {
            @Result(column = "id",property = "id",javaType = Long.class),
            @Result(property = "author",column = "author",javaType = String.class),
            @Result(property = "title",column = "title",javaType = String.class)
    })
    LearnResource queryLearnResouceById(@Param("id") Long id);

    @SelectProvider(type = LearnSqlBuilder.class,method = "queryLearnResouceByParams")
    List<LearnResource> queryLearnResouceList(Map<String, Object> params);


    class LearnSqlBuilder{
        public String queryLearnResouceByParams(final Map<String,Object> params){
            StringBuffer sql = new StringBuffer();
            sql.append("select * from learn_resource where 1=1");
            if (!StringUtil.isNull((String)params.get("author"))){
                sql.append("and author like %").append((String)params.get("author")).append("%");
            }
            if (!StringUtil.isNull((String)params.get("title"))){
                sql.append("and title like %").append((String)params.get("title")).append("%");
            }
            System.out.println("查询sql="+sql.toString());
            return sql.toString();
        }

        //删除的方法
        public String deleteByids(@Param("ids") final String[] ids){
            StringBuffer sql = new StringBuffer();
            sql.append("DELETE FROM learn_resource WHERE id in(");
            for (int i=0;i<ids.length;i++){
                if (i==ids.length-1){
                    sql.append(ids[i]);
                }else {
                    sql.append(ids[i]).append(",");
                }
            }
            sql.append(")");
            return sql.toString();
        }

    }
}
